/**
 * Project: saasoa
 * Copyright 2013  Top Digital Technology  Co. Ltd.
 * All right reserved.
 */
package com.topdt.jbpm.service.cmd;

import java.util.Date;

import org.jbpm.api.TaskQuery;
import org.jbpm.api.task.Task;

import com.thx.common.util.ThreadLocalContext;
import com.topdt.jbpm.model.HistTodo;
import com.topdt.jbpm.model.Todo;
//import com.topdt.oa.sms.service.SendRecordService;

/**
 * 建立日期 : 2013-6-27 下午2:48:36<br>
 * 作者 : balance<br>
 * 模块 : <br>
 * 描述 : <br>
 * 修改历史: 序号 日期 修改人 修改原因 <br>
 * 1 <br>
 * 2 <br>
 */
public class GoBackWorkCmd extends JumpWorkCmd {

	public GoBackWorkCmd(String todoId) {
		this.todoId = todoId;
	}

	@Override
	protected JumpResult doExecute() {

		creatResult();

		// 1、找见要退回的环节及历史处理人
		getPreTask();

		// 2、动态创建路径
		addOutcome();

		// 3、退回任务
		completeTask();

		// 4、删除动态创建的路径
		removeOutcome();

		return result;
	}

	protected void completeTask() {
		if (toActivity != null && histTodos != null) {
			String users = "";
			for (HistTodo todo : histTodos) {
				users = users + todo.getToUser() + ",";
			}

			if (users.endsWith(","))
				users = users.substring(0, users.length() - 1);

			getDestUsers();

			if (isFirstActivity(toActivity)) {// 第一个环节，待办特殊处理
				if(superTask!=null){
					taskService.completeTask(task.getId());
					taskService.completeTask(superTask.getId(), outCome, taskVars);	
				}else{
					taskService.completeTask(task.getId(), outCome, taskVars);					
				}

				String pId = task.getExecution().getProcessInstance().getId();

				TaskQuery query = jbpmService.getTaskService().createTaskQuery();
				Task task = query.processInstanceId(pId).activityName(toActivity.getName()).assignee(users)
						.uniqueResult();

				Todo nTodo = new Todo();
				nTodo.setArriveTime(new Date());
				nTodo.setFromUserId(todo.getToUserId());
				nTodo.setActivityName(toActivity.getName());
				nTodo.setToUserId(users);
				nTodo.setWorkId(work.getWorkId());
//				nTodo.setTodoTitle(work.getWorkTitle() + "[退回]");
				nTodo.setTodoTitle(work.getWorkTitle());//liujie edit 20160124
				nTodo.setTaskId(task.getId());
				nTodo.setState(Todo.STATE_READY);
				nTodo.setBatch(System.currentTimeMillis());
				nTodo.setAppName(todo.getAppName());
				nTodo.setPreActivityName(work.getCurActivityName());
				String mode = histTodos.get(0).getTodoMode();
				nTodo.setTodoMode(mode);
				nTodo.setCompanyId(todo.getCompanyId());

				jbpmService.getTodoDao().save(nTodo);

			} else {
				taskVars.put(toActivity.getName() + "_assginee", users);
				taskVars.put("isGoBack_", new Boolean(true));
				if(superTask!=null){
					taskService.completeTask(task.getId());
					taskService.completeTask(superTask.getId(), outCome, taskVars);	
				}else{
					taskService.completeTask(task.getId(), outCome, taskVars);					
				}
			}

			jbpmService.getTodoDao().delete(todoId);
			/*
			 * 更新待办短信记录表中的状态
			 * wangxufeng add 2014-01-09
			 */
			/*SendRecordService recordService = (SendRecordService)SysContext.getBean(SendRecordService.class);
			recordService.updateType(todoId);*/
			// add over
			HistTodo hTodo = new HistTodo(todo);
			hTodo.setState(Todo.STATE_BACK);// 退回去了
			hTodo.setDestActivityName(toActivity.getName());
			hTodo.setFinishTime(new Date());
			jbpmService.getHistTodoDao().save(hTodo);

			work.setCurActivityName("[" + toActivity.getName() + ":" + this.destUsers.get(0).getName() + "]");
			jbpmService.getWorkDao().update(work);
			//要退回历史环节的待办没有执行getNextCircs()方法，没有执行"ThreadLocalContext.remove("next_circs");"
			//造成退回来以后在线程变量中还有要退回历史环节的信息
			ThreadLocalContext.remove("next_circs");

			defaultResult();
		} else {
			creatResult();
			result.setJumpAble(false);
		}
	}
}
